<template>
  <div class="history">
    <van-nav-bar
      title="历史浏览记录"
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>

    <!-- 列表 -->
    <div class="essay-content">
      <li
        v-for="(item, index) in historyList"
        :key="item._id"
        @click="$router.push({path: '/article',
            query: { id: item._id, title: item.title },
          })
        "
      >
        <p>{{ item.title }}</p>
        <div
          :class="{
            none: item.poster_type == 1,
            articlesingleImg: item.poster_type == 2,
            articlemoreImg: item.poster_type == 3,
          }"
        >
          <img :src="img" v-for="img in item.imageSrc" />
        </div>
        <p>{{ item.time }}</p>
      </li>
    </div>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad">
      <van-cell v-for="item in historyList" :key="item._id" />
    </van-list>
  </div>
</template>

<script>
import { Tab, Tabs, List, PullRefresh } from "vant";
import { history } from "@/api/user.js";
import { mapState } from "vuex";
export default {
  name: "history",
  components: {
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [List.name]: List,
    [PullRefresh.name]: PullRefresh,
  },
  data() {
    return {
      historyList: [],
      skip: 0,
      limit: 20,
      list: [],
      loading: false,
      finished: false,
    };
  },
  computed: {
    ...mapState(["uid"]),
  },
  created() {
    this.history();
  },
  methods: {
    history() {
      let {uid} = this;
      history({
        uid,
        skip: this.skip,
        limit: this.limit,
      }).then((res) => {
        console.log(res);
        this.historyList = res.data;
      });
    },

     onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1);
        }

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 30) {
          this.finished = true;
        }
      }, 1000);
    },
     onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
  },
};
</script>

<style lang="less" scoped>
// 文章列表
.essay-content {
  .home-content-list-bto {
    display: flex;
    justify-self: start;
  }
}
.essay-content {
  overflow: hidden;
  img {
    max-width: 30%;
  }
}
</style>